package xc.doughnut.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import xc.doughnut.service.IMySQLService;

import java.util.Arrays;

/**
 * MyBatis-Plus MySQL代码生成类
 *
 * @author XuChao
 */
public class MySQLGenerator {

	// 数据表名
	private static final String AUTHOR = "XuChao";
	// 数据库ip
	public static final String DATABASE_IP = "localhost";
	// 数据库账号
	public static final String MYSQL_DB_USERNAME = "root";
	// 数据库密码
	public static final String MYSQL_DB_PASSWORD = "2000";
	// MYSQL数据库端口
	public static final String MYSQL_DB_PORT = "3306";
	// 数据库名
	public static final String MYSQL_DATABASE_NAME = "reggie";
	// 输出路径
	private static final String OUTPUT_DIR = "/src/main/java";
	// 设置目录结构,将生成的代码放在该目录下
	private static final String GENERATOR_PARENT_DIR = "com.egintra";
	// 生成的表,用`,`分割
	private static String[] TABLE_NAMES = new String[] {
			"frm_code","frm_codetype","sys_department"
	};
	// 是否将该库表全部生成
	private static final boolean ALL_TABLES_GENERATOR = true;

	public static final String URL_SUFFIX = "seUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC" +
			"&allowPublicKeyRetrieval=true";
	// 默任包结构
	private static final String CONTROLLER_DEF = "controller";
	private static final String ENTITY_DEF = "entity";
	private static final String MAPPER_DEF = "mapper";
	private static final String SERVICE_IMPL_DEF = "service.impl";
	private static final String SERVICE_DEF = "service";

	// TODO 默认生成entity，需要生成DTO修改此变量
	// 一般情况下要先生成 DTO类 然后修改此参数再生成 PO 类。
	private static final Boolean IS_DTO = false;

	public static void main(String[] args) {
		// 代码生成器
		AutoGenerator mpg = new AutoGenerator();
		// 选择 freemarker 引擎，默认 Velocity
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		gc.setFileOverride(true);
		//生成路径
		gc.setOutputDir(System.getProperty("user.dir") + OUTPUT_DIR);
		gc.setAuthor(AUTHOR);
		gc.setOpen(false);
		gc.setSwagger2(false);
		gc.setServiceName("%sService");
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);

		if (IS_DTO) {
			gc.setSwagger2(true);
			gc.setEntityName("%sDTO");
		}
		mpg.setGlobalConfig(gc);

		// MySQL
		DataSourceConfig dsc = new DataSourceConfig();
		String url = "jdbc:mysql://" + DATABASE_IP + ":" + MYSQL_DB_PORT + "/" + MYSQL_DATABASE_NAME;
		if (URL_SUFFIX != null && !"".equals(URL_SUFFIX)) {
			url += "?" + URL_SUFFIX;
		}
		dsc.setDbType(DbType.MYSQL)
				.setDriverName("com.mysql.cj.jdbc.Driver")
				.setUrl(url)
				.setUsername(MYSQL_DB_USERNAME)
				.setPassword(MYSQL_DB_PASSWORD);
		mpg.setDataSource(dsc);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent(GENERATOR_PARENT_DIR);
		pc.setController(CONTROLLER_DEF);
		pc.setService(SERVICE_DEF);
		pc.setServiceImpl(SERVICE_IMPL_DEF);
		pc.setXml(MAPPER_DEF);
		pc.setEntity(ENTITY_DEF);
		mpg.setPackageInfo(pc);

		// 设置模板
		TemplateConfig tc = new TemplateConfig();
		mpg.setTemplate(tc);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		// strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
		strategy.setEntityLombokModel(true);
		strategy.setRestControllerStyle(true);
		strategy.setEntityLombokModel(true);
		if (ALL_TABLES_GENERATOR) {
			// 将全部表生成
			IMySQLService isc = new IMySQLService(MYSQL_DATABASE_NAME);
			TABLE_NAMES = isc.getStrArrTables();
		}
		strategy.setInclude(TABLE_NAMES);
		strategy.setControllerMappingHyphenStyle(true);
		strategy.setTablePrefix(pc.getModuleName() + "_");
		mpg.setStrategy(strategy);
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		mpg.execute();

		// 自动填充字段配置
		 strategy.setTableFillList(Arrays.asList(
				new TableFill("create_date", FieldFill.INSERT),
				new TableFill("change_date", FieldFill.INSERT_UPDATE),
				new TableFill("modify_date", FieldFill.UPDATE)
		));
		mpg.setStrategy(strategy);
		mpg.execute();
	}

}
